#include<bits/stdc++.h>
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define rept(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) rept(i,0,n)
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define il inline
#define db double
#define ld long double
#define re return
#define F first
#define S second
#define int long long
using namespace std;
template<typename T>void Mx(T &a,T b){if(a<b)a=b;}
template<typename T>void Mi(T &a,T b){if(a>b)a=b;}
const int MOD=998244353;
const int INF=1000000000000000000;
void FILEIO(string s){
    freopen((s+".in").c_str(),"r",stdin);
    freopen((s+".out").c_str(),"w",stdout);
}
const int N=1<<18;
int n,m,tp;
int a[N],c[N],tg[19][N];
int lp,fs[19][N],rp[N],cnt[19][N];
void dfs0(int d,int s,int p){
    if(!d){
        rp[s]=p;fs[d][s]=-1;cnt[d][s]=s+1;
        re;
    }
    int sp=tg[d-1][s];
    if(sp){
        dfs0(d-1,s<<1|1,max(p,d));
        dfs0(d-1,s<<1,p);
    }
    else{
        dfs0(d-1,s<<1,max(p,d));
        dfs0(d-1,s<<1|1,p);
    }
    fs[d][s]=-1;cnt[d][s]=cnt[d-1][s<<1]+cnt[d-1][s<<1|1];
}
int dfs(int d,int s,int p){
    if(d==0){
        cnt[d][s]=(a[s]>=rp[s])*(s+1);
        // cout<<"add "<<d<<" "<<s<<" "<<cnt[d][s]<<" "<<rp[s]<<"\n";
        re fs[d][s]=a[s];
    }
    if(!cnt[d][s])cnt[d-1][s<<1]=cnt[d-1][s<<1|1]=0;
    if((p>>(d-1))&1)dfs(d-1,s<<1|1,p);
    else dfs(d-1,s<<1,p);
    int xl=fs[d-1][s<<1],xr=fs[d-1][s<<1|1];
    int sp=tg[d-1][s];
    if(sp){
        if(xr>=d)cnt[d-1][s<<1]=0;
        if(xr!=-1&&xr<d)cnt[d-1][s<<1|1]=0;
        cnt[d][s]=cnt[d-1][s<<1]+cnt[d-1][s<<1|1];
        if(xr==-1||xr>=d)re fs[d][s]=xr;
        re fs[d][s]=xl;
    }
    else{
        if(xl>=d)cnt[d-1][s<<1|1]=0;
        if(xl!=-1&&xl<d)cnt[d-1][s<<1]=0;
        cnt[d][s]=cnt[d-1][s<<1]+cnt[d-1][s<<1|1];
        if(xl==-1||xl>=d)re fs[d][s]=xl;
        re fs[d][s]=xr;
    }
}
vi getans(vi vec){
    vi res;
    int x=0;
    dfs0(lp,0,0);
    rep(i,1<<lp){
        dfs(lp,0,i);
        // cout<<i<<" "<<lp<<" : ";rep(j,1<<lp)cout<<cnt[0][j]<<" ";cout<<"\n";
        // cout<<i<<" "<<lp<<" : ";rep(j,1<<lp)cout<<fs[0][j]<<" ";cout<<"\n";
        // cout<<cnt[lp][0]<<"\n";
        while(x<sz(vec)&&vec[x]==i+1)res.pb(cnt[lp][0]),x++;
    }
    re res;
}
int rans[N];
void calc(){
    vector<vector<pii>>v(19,vector<pii>(0));
    rep(i,m){
        int cl=((c[i]==1)?0:(__lg(c[i]-1)+1));
        v[cl].pb({c[i],i});
    }
    rep(i,19)if(sz(v[i])){
        vi vv;
        sort(all(v[i]));
        for(pii j:v[i])vv.pb(j.F);
        lp=i;
        vi rv=getans(vv);
        rep(j,sz(vv))rans[v[i][j].S]=rv[j];
    }
    int ans=0;
    rep(i,m)ans^=(i+1)*rans[i];
    cout<<ans<<"\n";
}
void run(){
    cin>>n>>m;
    rep(i,n)cin>>a[i];
    rep(i,m)cin>>c[i];
    tp=((n==1)?0:(__lg(n-1)+1));
    rep(i,tp){
        string s;
        cin>>s;
        rep(j,1<<(tp-1-i))tg[i][j]=s[j]-'0';
    }
    int t;
    cin>>t;
    rep(_,t){
        // cout<<"case "<<_<<":\n";
        int x[4];
        rep(i,4)cin>>x[i];
        rep(i,n)a[i]^=x[(i+1)%4];
        calc();
        rep(i,n)a[i]^=x[(i+1)%4];
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    FILEIO("arena");
    int T=1;
    // cin>>T;
    while(T--)run();
    re 0;
}

/*
14:37 t1 ok
14:39 t1 data ok
15:00 t3 data ok
15:08 t3 pai ok
15:36 t2 ok
15:39 t2 data ok
15:45 WC
15:51 t4 idea
16:44 t4 brute O(Tnm) ok
16:55 t4 brute data ok
18:26
*/